# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 消费者 Consumer（接收消息）.py
# @Author: dongguangwen
# @Date  : 2025-04-12 12:10
import pika

# 连接到 RabbitMQ
credentials = pika.PlainCredentials('admin', 'password')
connection = pika.BlockingConnection(
    pika.ConnectionParameters('192.168.1.119', credentials=credentials, virtual_host='my_vhost'))
channel = connection.channel()

# 确保监听队列已存在
channel.queue_declare(queue='hello')


# 回调函数：每次收到消息时调用
def callback(ch, method, properties, body):
    print(f"[x] 收到消息：{body.decode()}")


# 订阅队列
channel.basic_consume(queue='hello',
                      on_message_callback=callback,
                      auto_ack=True)

print('[*] 等待消息，按 Ctrl+C 退出')
channel.start_consuming()
